package edu.ues21.tcs.ecommerce2011.mvc.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import edu.ues21.tcs.ecommerce2011.mvc.dao.MarcaDAO;
import edu.ues21.tcs.ecommerce2011.mvc.model.Marca;

/**
 * 
 * @author Acunia Marco.
 */
public class EditMarcaController extends HttpServlet {

	ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
			"/BeanLocations.xml");

	MarcaDAO mDao = (MarcaDAO) appContext.getBean("marcaDAO");

	private static final long serialVersionUID = 1L;

	
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String idmarca = request.getParameter("id");
		Integer idint = Integer.parseInt(idmarca);
		Marca m = mDao.getMarca(idint);
		request.setAttribute("marca", m);
		request.getRequestDispatcher("view/EditMarca.jsp").forward(request,
				response);
	}

	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String nombre = request.getParameter("marca");
		String idmarca = request.getParameter("idmarca");
		System.out.println("++++++++++++++++++++++++++++ " + nombre + idmarca);
		if (nombre != null) {
			Marca m = new Marca();
			Integer idint = Integer.parseInt(idmarca);
			m.setIdmarca(idint);
			m.setNombre(nombre);
			mDao.save(m);
		}

		response.sendRedirect("CRUDMarcas.do");

	

	}


	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>

}
